//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2006
//
// Set routing graph node id.
//

# include "StdAfx.h"
# include "Resource.h"
# include "SetRGNodeId.h"
# include "UndoRedo.h"

class CSetRGNodeIdAction : public CEditAction {
  public:
	CSetRGNodeIdAction (rgnode_t * _pNode, DWORD _dwNodeID) :
		m_pNode (_pNode),
		m_dwNodeID (_dwNodeID)
	{}

	virtual void GetName (string_t & _str) const;
	virtual cu_t CanUndo () const {return cuYes;}
	virtual bool Do ();
	virtual void Undo ();
	virtual void Redo ();

  private:
	rgnode_t * const m_pNode;
	DWORD m_dwNodeID;
};

void CSetRGNodeIdAction::GetName (string_t & _str) const {
	_str.append ("set node id");
}

bool CSetRGNodeIdAction::Do () {
	CCritSec::CLock lock (g_cs);

	if (g_map.dwMaxNodeID < m_dwNodeID)
		g_map.dwMaxNodeID = m_dwNodeID;
	std::swap (m_pNode->dwNodeID, m_dwNodeID);

	return true;
}

void CSetRGNodeIdAction::Undo () {
	CCritSec::CLock lock (g_cs);

	CSetRGNodeIdAction::Do ();
}

void CSetRGNodeIdAction::Redo () {
	CSetRGNodeIdAction::Do ();
}

/////////////////////////////////////////////////

void SetRGNodeId (rgnode_t * _pNode, DWORD _dwNodeID) {
	PerformEditAction (new CSetRGNodeIdAction (_pNode, _dwNodeID));
}
